﻿Imports System.ServiceModel.DomainServices.Client
Imports LullabyFullDeCaixa.Web

Partial Public Class ConfigReportEstatHostal
    Inherits ConfigReportAbstract

    Public Sub New()
        MyBase.New(My.Resources.StringLibrary.InformeEstatHostal, EnumTipusReport.EstatHostal)
        InitializeComponent()

        If Contexte.IsHostalsLoaded AndAlso Contexte.IsUsuarisLoaded Then
            OmpleDadesBuscador()
        Else
            Me.Activity.IsActive = True

            AddHandler Contexte.HostalsCarregats, AddressOf HostalsCarregats
            AddHandler Contexte.UsuarisCarregats, AddressOf UsuarisCarregats

            Contexte.LoadUsuaris()
            Contexte.LoadHostals()
        End If

    End Sub

#Region "Generar"

    Private Sub GenerarReport(IdHostal As Integer, IdUsuari As Integer, ByVal DataInici As Nullable(Of DateTime), ByVal DataFi As Nullable(Of DateTime))
        Dim uri As String = BASE_URI & "?Report=" & Me.tipusReport

        If DataInici IsNot Nothing Then
            uri &= "&DataInici=" & DataInici.Value.Ticks
        End If

        If DataFi IsNot Nothing Then
            uri &= "&DataFi=" & DataFi.Value.Ticks
        End If

        'afegim l'element filtrat
        uri &= "&IdHostal=" & IdHostal
        uri &= "&IdUsuari=" & IdUsuari

        System.Windows.Browser.HtmlPage.Window.Eval("window.open('" & uri & "', '_blank')")
    End Sub

    Public Overrides Sub Generar()
        GenerarReport(Me.cboHostals.SelectedItem.Id, Me.cboUsuaris.SelectedItem.id, Me.dtpDataInici.SelectedDate, Me.dtpDataFi.SelectedDate)
    End Sub

#End Region

#Region "Carregar Dades"
    Private Sub OmpleDadesBuscador()
        If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
            Dim LListaHostals As List(Of Hostal) = Contexte.Hostals.ToList
            LListaHostals.Insert(0, New Hostal() With {.Id = -1, .Nom = ""})
            Me.cboHostals.ItemsSource = LListaHostals

            Dim LListaUsuaris As List(Of Usuari) = (From U As LullabyFullDeCaixa.Web.Usuari In Contexte.Usuaris Where U.IdTipusUsuari <> EnumTipusUsuari.Neteja).ToList
            LListaUsuaris.Insert(0, New Usuari() With {.Id = -1, .Nom = ""})
            Me.cboUsuaris.ItemsSource = LListaUsuaris
        Else
            Me.cboHostals.ItemsSource = Contexte.HostalsPerUsuariActual

            Dim LListaUsuaris As List(Of Web.Usuari)
            Dim LListaHostalsAssignats As List(Of Web.Hostal) = (From UH As Web.UsuariHostal In Contexte.UsuariActual.UsuariHostals Select UH.Hostal).ToList
            LListaUsuaris = (From UH As Web.UsuariHostal In Contexte.DomainContext.UsuariHostals Where LListaHostalsAssignats.Contains(UH.Hostal) And UH.Usuari.IdTipusUsuari <> EnumTipusUsuari.Neteja Select UH.Usuari).Distinct.ToList
            LListaUsuaris.Insert(0, New Usuari() With {.Id = -1, .Nom = ""})
            'LListaUsuaris.AddRange((From U As Web.Usuari In Contexte.Usuaris Where U.IdTipusUsuari = EnumTipusUsuari.Administrador))

            Me.cboUsuaris.ItemsSource = LListaUsuaris '{Contexte.UsuariActual}
        End If

        Me.cboHostals.SelectedItem = cboHostals.Items(0)
        Me.cboUsuaris.SelectedItem = cboUsuaris.Items(0)
        Me.dtpDataInici.SelectedDate = Today.AddDays(-7).ToShortDateString
        Me.dtpDataFi.SelectedDate = Today.ToShortDateString
    End Sub

    Private Sub HostalsCarregats(Resultat As Boolean, Missatge As String)
        RemoveHandler Contexte.HostalsCarregats, AddressOf HostalsCarregats

        If Contexte.IsHostalsLoaded AndAlso Contexte.IsUsuarisLoaded Then
            Me.Activity.IsActive = False
            OmpleDadesBuscador()
        End If
    End Sub

    Private Sub UsuarisCarregats(Resultat As Boolean, Missatge As String)
        RemoveHandler Contexte.UsuarisCarregats, AddressOf UsuarisCarregats

        If Contexte.IsHostalsLoaded AndAlso Contexte.IsUsuarisLoaded Then
            Me.Activity.IsActive = False
            OmpleDadesBuscador()
        End If
    End Sub
#End Region


End Class
